package com.ruoyi.web.controller.sys.workHour;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.BusiProjectItem;
import com.ruoyi.system.domain.PersonItem;
import com.ruoyi.system.domain.ProjectAndItem;
import com.ruoyi.system.domain.ProjectAndPerson;
import com.ruoyi.system.service.PersonItemService;
import com.ruoyi.system.service.ProjectItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("工时分配")
@RestController
@RequestMapping("/sys/WorkHourDistribution")
public class workHourDistribution extends BaseController {

    @Autowired
    ProjectItemService projectItemService;


    @Autowired
    PersonItemService personItemService;

    /**
     * 显示所有子项目
     *
     * @return
     */
    @ApiOperation("在工时分配中显示所有子项目")
    @PostMapping("/showAll")
    public TableDataInfo showAll() {
        //开启分页
        startPage();
        List<BusiProjectItem> busiProjectItems = projectItemService.show();
        for (BusiProjectItem busiProjectItem : busiProjectItems) {
            //计算Prd剩余的工时
            busiProjectItem.setLeftPrdWorkhours(busiProjectItem.getPrdWorkhours() - busiProjectItem.getPrdCompleteWorkhours());
            //计算Pso剩余的工时
            busiProjectItem.setLeftPsoWorkhours(busiProjectItem.getPsoWorkhours() - busiProjectItem.getPsoCompleteWorkhours());
            //计算Srd剩余的工时
            busiProjectItem.setLeftSrdWorkhours(busiProjectItem.getSrdWorkhours() - busiProjectItem.getSrdCompleteWorkhours());
            //计算外部工时剩余的工时
            busiProjectItem.setLeftOthersWorkhours(busiProjectItem.getOthersWorkhours() - busiProjectItem.getOthersCompleteWorkhours());
        }
        //返回分页结果
        return getDataTable(busiProjectItems);
    }

    /**
     * 显示所有员工的子项目分配情况
     */
    @ApiOperation("显示所有子项目分配情况")
    @PostMapping("/showItem")
    public TableDataInfo showItem() {
        startPage();
        return getDataTable(projectItemService.showAll());
    }

    /**
     * 显示当前子项目的人员工时情况
     */
    @ApiOperation("显示当前子项目的人员工时情况")
    @PostMapping("/showItemPerson")
    public TableDataInfo showItemPerson(BusiProjectItem busiProjectItem) {
        //开始分页
        startPage();
        return getDataTable(personItemService.showAll(busiProjectItem.getItemName()));
    }

    /**
     * 多条件模糊查询
     */
    @ApiOperation("多条件模糊查询")
    @PostMapping("/find")
    public TableDataInfo find(@ApiParam(name = "", value = "传入项目名称", required = false)
                                  @RequestParam String projectName,
                              @ApiParam(name = "", value = "传入项目编码", required = false)
                                  @RequestParam String projectCode,
                              @ApiParam(name = "", value = "传入子项目的名称", required = true)
                                  @RequestParam String itemName) {
        //开始分页
        startPage();
        return getDataTable(projectItemService.find(projectName, projectCode, itemName));
    }

    /**
     * 分配PRD工时，SRD工时，外部工时，PSO工时
     */
    @ApiOperation("分配PRD工时，SRD工时，外部工时，PSO工时，当月分配工时加到以分配工时中")
    @PostMapping("/distribution")
    public AjaxResult distribution(@ApiParam(name = "", value = "传入分配后工时的子项目", required = true)
                                   @RequestBody BusiProjectItem busiProjectItem,
                                   @ApiParam(name = "", value = "传入员工分配工时的列表", required = true)
                                   @RequestBody List<PersonItem> personItems
    ) {
        busiProjectItem.setLeftPsoWorkhours(busiProjectItem.getPsoWorkhours() - busiProjectItem.getPsoCompleteWorkhours());
        busiProjectItem.setLeftSrdWorkhours(busiProjectItem.getSrdWorkhours() - busiProjectItem.getSrdCompleteWorkhours());
        busiProjectItem.setLeftPrdWorkhours(busiProjectItem.getPrdWorkhours() - busiProjectItem.getPrdCompleteWorkhours());
        busiProjectItem.setLeftOthersWorkhours(busiProjectItem.getOthersWorkhours() - busiProjectItem.getOthersCompleteWorkhours());
        projectItemService.updateItem(busiProjectItem);
        for (PersonItem personItem : personItems) {
            personItemService.update(personItem);
        }
        return AjaxResult.success(1);
    }

    /**
     * 显示已分配工时的详情
     */
    @ApiOperation("显示已分配工时的详情")
    @PostMapping("/showPeopleTime")
    public TableDataInfo showPeopleTime(@ApiParam(name = "", value = "传入ProjectAndItem类", required = true) @RequestBody ProjectAndItem projectAndItem) {
        startPage();
        return getDataTable(personItemService.showPeopleTime(projectAndItem.getItemId()));
    }

    /**
     * 核减工时
     */
    @ApiOperation("核减工时")
    @PostMapping("/update")
    public AjaxResult update(
            @ApiParam(name = "", value = "传入ProjectAndPerson类", required = true)
            @RequestBody ProjectAndPerson projectAndPerson,

            @ApiParam(name = "", value = "传入核减工时", required = true)
            @RequestParam Double updateTime,

            @ApiParam(name = "", value = "传入核减原因", required = true)
            @RequestParam String remarks) {
        PersonItem personItem = personItemService.findById(projectAndPerson.getId());
        personItem.setWorkTime(updateTime);
        personItem.setRemarks(remarks);
        personItemService.update(personItem);
        return null;
    }
}
